<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="Connect.*"%>
<%
	// Redirect to the home page if the user is not a manager or employee.
	if (session.getAttribute("user_type") == null) {
		response.sendRedirect("no_permission.jsp");
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//Dth HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dth">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
<script language="javascript" type="text/javascript">
	function editClicked(button) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "edit_item.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "id");
		data.setAttribute('value', button.title);
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
	function createAuctionClicked(button) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "create_auction.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "id");
		data.setAttribute('value', button.title);
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
	function viewAuctionsClicked(button) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "view_auctions.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "id");
		data.setAttribute('value', button.title);
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
</script>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<%
			//String auction_owner = (String)session.getAttribute("auction_user");
			String auction_owner = request.getParameter("customer");
			String username = "";

			if (auction_owner == null) {
				username = (String) session.getAttribute("logged_user");
			} else {
				username = auction_owner;
			}
		%>
		<h1>Personalized Recommendation List for <%=auction_owner %></h1>
		<%
			// Getting information and updating the database.
			java.sql.Connection conn = null;
			try {
				// Connect to the database.
				Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
				java.util.Properties sysprops = System.getProperties();
				sysprops.put("user", ConnectionInfo.myUserID());
				sysprops.put("password", ConnectionInfo.myPassword());
				conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);

				java.sql.Statement statement = conn.createStatement();
				java.sql.ResultSet rs;

				//LET US GET THE TYPES OF ITEMS THAT THE USER BID ON 

				// 				rs = statement.executeQuery("SELECT DISTINCT items.id, items.type" + " FROM bids JOIN items JOIN auctions"
				// 						+ " WHERE auctions.item = items.id" + " AND auctions.id = bids.auction_id" + " AND bidder_username = '" + username
				// 						+ "' ORDER BY bid_time DESC ");
				rs = statement
						.executeQuery("SELECT DISTINCT items.id, type FROM items JOIN (SELECT * FROM bids JOIN auctions ON auction_id=id WHERE bidder_username = '"
								+ username + "' ORDER BY bid_time DESC) AS ba ON items.id=item GROUP BY type");
				while (rs.next()) {
					java.sql.Statement statement2 = conn.createStatement();
					java.sql.ResultSet rs2 = statement2.executeQuery("SELECT * FROM items WHERE id!='" + rs.getString("items.id") + "' AND type='"
							+ rs.getString("type") + "'");
					while (rs2.next()) {
						java.sql.ResultSetMetaData columns = rs2.getMetaData();
		%>
		<table class="center item-table">
			<tr>
				<th class="left-text padding-5px"><a id=<%=" " + rs2.getString("id")%>>Item Id: <%=" " + rs2.getString("id")%></a></th>
				<th></th>
			</tr>
			<tr>
				<%@ page import="java.io.File"%>
				<%
					String imagePath = application.getRealPath("item_images") + "\\" + rs2.getString("id") + ".png";
								File file = new File(imagePath);
								if (!file.exists()) {
									//imagePath = application.getRealPath("item_images") + "\\" + "0" + ".png";
									imagePath = "images/logo.png";
								} else {
									// Runtime correctioin. Mostly has to do with local servers.
									imagePath = imagePath.substring(imagePath.indexOf("item_images"));
								}
				%>
				<td style="vertical-align: top" width="128" height="128"><img src="<%=imagePath%>" alt="item image" width="128" height="128"></td>
				<td>
					<table class="inner-table">
						<tr>
							<th>Name:</th>
							<td><%=" " + rs2.getString("name")%></td>
						</tr>
						<tr>
							<th>Year:</th>
							<td><%=" " + rs2.getString("year")%></td>
						</tr>
						<tr>
							<th>Type:</th>
							<td><%=" " + rs2.getString("type")%></td>
						</tr>
						<tr>
							<th>Copies Available:</th>
							<td><%=" " + rs2.getString("copies_available")%></td>
						</tr>
						<tr>
							<th>Copies Sold:</th>
							<td><%=" " + rs2.getString("copies_sold")%></td>
						</tr>
						<tr>
							<th>Description:</th>
							<td><%=" " + rs2.getString("description")%></td>
						</tr>
						<tr>
							<td></td>
							<td><table class="right">
									<tr>
										<%
											String user_type = (String) session.getAttribute("user_type");
													if (user_type.equalsIgnoreCase("0")) {
														out.println("<td><a href=\"#\" class=\"right button\" title=" + rs2.getString(columns.getColumnName(1))
																+ " onclick=\"return editClicked(this)\">Edit</a></td>");
													}
													if (user_type.equalsIgnoreCase("2")) {
														out.println("<td><a href=\"#\" class=\"right button\" title=" + rs2.getString(columns.getColumnName(1))
																+ " onclick=\"return createAuctionClicked(this)\">+ New Auction</a></td>");
													}
													out.println("<td><a href=\"#\" class=\"right button\" title=" + rs2.getString(columns.getColumnName(1))
															+ " onclick=\"return viewAuctionsClicked(this)\">View Auctions</a></td>");
										%>
									</tr>
								</table></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
		<br />
		<%
			}
				}
				// 				// LETS OPEN ANOTHER CONNECTION
				// 			java.sql.Connection	conn2 = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);
				// 			java.sql.Statement statement2 = conn2.createStatement();
				// 			java.sql.ResultSet rs2;
				// 			int counter = 0;
				// 			while(rs.next() && counter < 10){
				// 				String id = rs.getString("id");
				// 				String type = rs.getString("type");

				// 				rs2 = statement2.executeQuery("SELECT * FROM items WHERE id != '" +
				// 				id +"' AND type = '" + type + "'");

				// 				if(rs2.next()){
				// 					System.out.println(rs2.getString("name"));
				// 					System.out.println(rs2.getString("copies_available"));
				// 					/*

				// 					THIS IS WHERE YOU PRINT OUT THE ITEMS USING RESULT SET R2

				// 					*/

				// 					counter++;
				// 				}

				// 			}

			} catch (Exception e) {
				e.printStackTrace();
				out.print(e.toString());
			} finally {
				try {
					conn.close();
				} catch (Exception ee) {
				}
			}
		%>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>